package com.demo.servlet.demo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ddone
 * @date 2024/3/11-16:15
 */
@WebServlet(name = "/charset", value = "/charset")
public class CharsetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        System.out.println(req.getParameter("name"));
        System.out.println(req.getParameter("sex"));
        // 解决响应乱码问题
        resp.setContentType("text/html;charset=utf-8");
        /* resp.getWriter().write("调用成功"); */
        req.getRequestDispatcher("success.html").forward(req, resp);
    }
}
